package point;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class GetPoint {
	/*
	 * Vector<String> pointy; Vector<String> pointx;
	 */
	String pointy = "";
	String pointx = "";
	String apiurl = "http://openapi.map.naver.com/api/geocode.php?key=5f434d76b966da4ca9f57d40e4e2ee4e&encoding=euc-kr&coord=LatLng&query=";
	URL url;
	String urlstring;
	String title, description, tagname;

	public GetPoint(String url) {
		/*
		 * pointy = new Vector<String>(); pointx = new Vector<String>();
		 */
		this.urlstring = apiurl + url;
	}

	public Coordinate getPoint() {

		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();

			url = new URL(urlstring);

			InputStream in = url.openStream();

			xpp.setInput(in, "euc-kr");

			int eventType = xpp.getEventType();

			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {

					tagname = xpp.getName();
				} else if (eventType == XmlPullParser.TEXT) {
					if (!xpp.isWhitespace()) {
						if (tagname.equals("x")) {
							pointx = xpp.getText();
						} else if (tagname.equals("y")) {
							pointy = xpp.getText();
						}
					}
				}
				eventType = xpp.next();
			}

		} catch (XmlPullParserException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Coordinate point = new Coordinate(pointx, pointy);

		return point;
	}
}
